package Euler27;

import java.util.*;

import ReusableCode.PrimeFunctions;

public class QuadraticPrimes {
	public static void main(String[] args) {
		int bound = 1000000;
		int maxA=0, maxB=0, max=0;
		HashSet<Integer> primesHash = PrimeFunctions.GetPrimesHash(bound*2);
		List<Integer> primes = PrimeFunctions.GetPrimes(bound*2);
		
		int a=0;
		for(int i = 0; a < bound; i++)
		{
			a = primes.get(i);
			
			int b=0;
			for(int j = 0; b < bound; j++)
			{
				b = primes.get(j);
				
				for(int x = -1; x <= 1; x+=2)
				{
					for(int y = -1; y <= 1; y+=2)
					{
						int consec = getNumConsecutivePrimes(x * a, y * b, primesHash);
						if(consec > max)
						{
							maxA = x * a;
							maxB = y * b;
							max = consec;
						}
					}
				}
			}
			//System.out.println(a);
		}
		
		System.out.println("a is: " + maxA + ", b is: " + maxB + ", the number of consecutives is: " + max + ", and the coefficient product is: " + (maxA*maxB));
	}
	
	static int getNumConsecutivePrimes(int a, int b, HashSet<Integer> primes)
	{
		int n = 0;
		
		while(true)
		{
			int num = (int) ((n*n) + (a * n) + b);
			if(isPrime(num, primes))
			{
				n++;
			}
			else
			{
				break;
			}
		}
		
		return n;
	}
	
	static boolean isPrime(int num, HashSet<Integer> primes)
	{
		if(num > 1)
		{
			if(primes.contains(num))
			{
				return true;
			}
		}
		
		return false;
	}
}
